package com.baidu.sggc.service;

import com.baidu.sggc.data.req.CommentsReq;
import com.baidu.sggc.entity.Comments;
import com.baidu.sggc.entity.UserContext;
import com.baidu.sggc.mapper.CommentsMapper;
import com.baidu.sggc.core.common.utils.InputValidatorUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论 Service
 */
@Service
@RequiredArgsConstructor
public class CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 添加评论
     *
     * @param comments 评论对象
     * @return 是否成功
     */
    public void addComment(Comments comments) {
        if (!InputValidatorUtils.isValidDescription(comments.getComment())) {
            throw new IllegalArgumentException("长度不能超过100，且不能为空");
        }
        Integer rating = comments.getRating();
        if (rating == null || rating < 1 || rating > 5) {
            throw new IllegalArgumentException("评分不能为空且只能是1-5的整数");
        }
        String currentUserId = UserContext.getUserId();
        comments.setUserId(currentUserId);
        commentsMapper.insert(comments);
    }

    /**
     * 获取所有评论
     *
     * @return 评论列表
     */
    public List<Comments> getAllComments(CommentsReq commentsReq) {
        return commentsMapper.list(commentsReq);
    }
}
